//
//  YMHealthTestReportVC.m
//  YMMedical
//
//  Created by y s on 2023/7/21.
//

#import "YMHealthTestReportVC.h"
#import "YMTestReportTopInforCell.h"
#import "YMTestReportChartsCell.h"
#import "YMTestReportNextTimeCell.h"
#import "YMTestReportIntroduceCell.h"
#import "YMTestReportShareView.h"
#import "YMHomeTipInforAlert.h"
#import "YMHomeService.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface YMHealthTestReportVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) YMTestReportShareView *vShare;

@property (nonatomic, strong) NSDictionary *dictMain;

@end

@implementation YMHealthTestReportVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self loadData];
}

- (void)setUI{
    [self showNavTitle:KLanguage(@"dete_report")];
    WEAK_SELF;
    [SNavUtil rightIcon:self icon:@"tools_nav_share_icon" onClick:^{
        [weakSelf shareClick];
    }];

    if (_backHome){
        self.fd_interactivePopDisabled = YES;
        [SNavUtil leftIcon:self icon:@"tools_nav_left_back" onClick:^{
            [YMBridging goAppTabWithIndex:0];
        }];
    }
    [self.view addSubview:self.tvMain];
    
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [SThemeUtil refershHead:self.tvMain block:^{
        [weakSelf loadData];
    }];
    
     
}

- (void)loadData{
    WEAK_SELF;
    [YMHomeService requestHealthGetOneHealthRecordDetails:_detectionId success:^(NSDictionary *dictData) {
        [weakSelf.tvMain.mj_header endRefreshing];
        weakSelf.dictMain = dictData;
        weakSelf.vShare.dict = dictData;
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [weakSelf.tvMain.mj_header endRefreshing];
        [SHUD showInfo:message];
    }];
}

#pragma mark -- event action
- (void)shareClick{
    WEAK_SELF;
    YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
    alert.iconName = @"alert_tips_infor_icon";
    alert.strContent = KLanguage(@"share_report_hint_txt");
    alert.sureBlock = ^{
        UIImage *image = [SUIUtil imageOfView:weakSelf.vShare];
        [YMBridging goToShareWithTitle:@"" url:@"" image:image success:^{
            
        }];
    };
    [alert show];
    
}
- (void)downloadClick{
    
}
#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        YMTestReportTopInforCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMTestReportTopInforCell" forIndexPath:indexPath];
        cell.dict = self.dictMain;
        return cell;
    }else if (indexPath.section == 1){
        YMTestReportChartsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMTestReportChartsCell" forIndexPath:indexPath];
        cell.dictData = _dictMain;
        return cell;
    }else if (indexPath.section == 2){
        YMTestReportNextTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMTestReportNextTimeCell" forIndexPath:indexPath];
        cell.nextDate = [SDataUtil stringOf:self.dictMain[@"nextCollectDate"] valid:@""];
        return cell;
    }else if (indexPath.section == 3){
        YMTestReportIntroduceCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == nil) {
            cell = [[YMTestReportIntroduceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YMTestReportIntroduceCell"];
        }
        cell.strText = [SDataUtil stringOf:_dictMain[@"minorTermDescription"] valid:@""];
        return cell;
    }
    return [[UITableViewCell alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WEAK_SELF;
    if (indexPath.section == 0)
    {
        return [tableView fd_heightForCellWithIdentifier:@"YMTestReportTopInforCell" configuration:^(YMTestReportTopInforCell *cell) {
            cell.dict = weakSelf.dictMain;
        }];
    }else if (indexPath.section == 1)
    {
        return [YMTestReportChartsCell getCellHeight];
    }else if (indexPath.section == 2)
    {
        return [YMTestReportNextTimeCell getCellHeight];
    }else if (indexPath.section == 3)
    {
        return [tableView fd_heightForCellWithIdentifier:@"YMTestReportIntroduceCell" configuration:^(id cell) {
            if ([cell isKindOfClass:[YMTestReportIntroduceCell class]]){
                YMTestReportIntroduceCell *nCell = cell;
                nCell.strText = [SDataUtil stringOf:weakSelf.dictMain[@"minorTermDescription"] valid:@""];
            }
        }];
    }
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = YES;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tvMain registerClass:[YMTestReportTopInforCell class] forCellReuseIdentifier:@"YMTestReportTopInforCell"];
        [_tvMain registerClass:[YMTestReportChartsCell class] forCellReuseIdentifier:@"YMTestReportChartsCell"];
        [_tvMain registerClass:[YMTestReportNextTimeCell class] forCellReuseIdentifier:@"YMTestReportNextTimeCell"];
        [_tvMain registerClass:[YMTestReportIntroduceCell class] forCellReuseIdentifier:@"YMTestReportIntroduceCell"];
    }
    return _tvMain;
}
- (YMTestReportShareView *)vShare{
    if (_vShare == nil){
        _vShare = [[YMTestReportShareView alloc] init];
    }
    return _vShare;
}

@end
